# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 11:00:56 2020

@author: Yhkun
"""

from docx import Document
from docx.shared import Inches
import time
import requests
import json
import hyjianbao

# 创建word文档对象
document = Document()

from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn
import ycwb
from docx.shared import Pt
import dayin


# 添加标题
run = document.add_heading('',level=0).add_run(u"Yhkun新闻日报")#应用场景示例标题
run.font.name=u'微软雅黑'
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')


from docx.oxml.ns import qn
 
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
yiyan=json.loads(requests.get("https://v1.hitokoto.cn/?c=i").text)['hitokoto']
p = document.add_paragraph('今天是'+time.strftime('%Y{y}%m{m}%d{d}').format(y='年',m='月',d='日')+"    "+yiyan)

web_get = requests.get("https://tianqiapi.com/api?version=v61&appid=19341646&appsecret=Ik7qjzIS&city=广州")
web_sj = json.loads(web_get.text)

tem = web_sj['tem']
tem1 = web_sj['tem1']
tem2 = web_sj['tem2']
win = web_sj['win']
win_speed = web_sj['win_speed']
humidity = web_sj['humidity']
air_tips = web_sj['air_tips']
alarm_type = web_sj['alarm']['alarm_type']
alarm_level = web_sj['alarm']['alarm_level'] 
alarm_content = web_sj['alarm']['alarm_content']

if alarm_type == '':
    alarm = False
else:
    alarm = True


document.add_heading('天气你知', level=1)
s = document.add_paragraph("当前温度:{} 今日最高温度:{} 今日最低温度:{}".format(tem,tem1,tem2))
if alarm == False:
    s.add_run("  今天的天气不错哦！")
else:
    s.add_run(alarm_type+":"+alarm_level+" "+alarm_content)

s.add_run("  "+air_tips)

document.add_heading('新闻一览', level=1)
for gh in ycwb.getycwb():
    document.add_heading(gh['bt'][5:], level=3)
    kk= document.add_paragraph(gh['nr'])
    style = document.styles['Normal']
    font = style.font
    font.size = Pt(8)
document.add_heading('新鲜事', level=1)
for jg in hyjianbao.get_newshy():
    kk= document.add_paragraph(jg)
    style = document.styles['Normal']
    font = style.font
    font.size = Pt(10)



document.save('news.docx')
print("新闻数据保存成功")
time.sleep(10)

dayin.printer_loading("news.docx")